import { StoreOptions } from 'vuex'
import ACCESS_ENUM from '@/access/accessEnum'
import { UserControllerService } from '../../generated'

export default {
  namespaced: true,
  state: () => ({
    //状态
    loginUser: {
      userName: '未登录',
      userRole: ACCESS_ENUM.NOT_LOGIN
    }
  }),
  mutations: {
    //回调
    updateUser(state, payload) {
      state.loginUser = payload
    }
  },
  actions: {
    //方法
    async getLoginUser({ commit, state }, payload) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet()
      if (res.code === 0) {
        console.log(res.data)
        commit('updateUser', res.data)
      } else {
        commit('updateUser', {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN
        })
      }
    }
  }
} as StoreOptions<any>
